home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1986 / 04 / brown.pro < prev    next >
Text File  |  1986-04-30  |  9KB  |  257 lines

  1. ; File: BOYER.LIB
  2.  
  3. (PUTD 'DEFUN '(NLAMBDA (NAM$ EXP$) (PUTD NAM$ EXP$) NAM$))
  4.  
  5.  
  6.  
  7. (DEFUN VARIABLEP (LAMBDA (V)
  8.   (* determine if V is a variable: if it starts with lower-case
  9.       it is *)
  10.   ((ATOM V)
  11.     (GREATERP (ASCII (CAR (UNPACK V))) (ASCII `)) )
  12.   NIL ))
  13.  
  14. (DEFUN LPAR (LAMBDA (R)
  15.   (* return left parent *)
  16.   (CAR R) ))
  17.  
  18. (DEFUN LLIT# (LAMBDA (R)
  19.   (* return left litteral number *)
  20.   (CADR R) ))
  21.  
  22. (DEFUN RPAR (LAMBDA (R)
  23.   (* return right parent *)
  24.   (CADDR R) ))
  25.  
  26. (DEFUN RLIT# (LAMBDA (R)
  27.   (* return right litteral number *)
  28.   (CAR (CDDDR R)) ))
  29.  
  30. (DEFUN NLITS (LAMBDA (R)
  31.   (* return number of litterals *)
  32.   (CADR (CDDDR R)) ))
  33.  
  34. (DEFUN MAXNDX (LAMBDA (R)
  35.   (* return maximum index *)
  36.   (CADDR (CDDDR R)) ))
  37.  
  38. (DEFUN BINDINGS (LAMBDA (R)
  39.   (* return the bindings *)
  40.   (CAR (CDDDR (CDDDR R))) ))
  41.  
  42. (DEFUN SETLPAR (LAMBDA (R V)
  43.   (* set left parent *)
  44.   (RPLACA R V) ))
  45.  
  46. (DEFUN SETLLIT# (LAMBDA (R V)
  47.   (* set left litteral number *)
  48.   (RPLACA (CDR R) V) ))
  49.  
  50. (DEFUN SETRPAR (LAMBDA (R V)
  51.   (* set right parent *)
  52.   (RPLACA (CDDR R) V) ))
  53.  
  54. (DEFUN SETRLIT# (LAMBDA (R V)
  55.   (* set right litteral number *)
  56.   (RPLACA (CDDDR R) V) ))
  57.  
  58. (DEFUN SETNUMLITS (LAMBDA (R V)
  59.   (* set number of litterals *)
  60.   (RPLACA (CDR (CDDDR R)) V) ))
  61.  
  62. (DEFUN SETMAXNDX (LAMBDA (R V)
  63.   (* set maximum index *)
  64.   (RPLACA (CDDR (CDDDR R)) V) ))
  65.  
  66. (DEFUN SETBINDINGS (LAMBDA (R V)
  67.   (* set bindings *)
  68.   (RPLACA (CDDDR (CDDDR R)) V) ))
  69.  
  70. (DEFUN INRECP (LAMBDA (R)
  71.   (* is R an input record? *)
  72.   (NULL (RPAR R)) ))
  73.  
  74. (DEFUN LEQP (LAMBDA (X Y)
  75.   (* is X less than or equal to Y ? *)
  76.   (NOT (GREATERP X Y)) ))
  77.  
  78. (DEFUN NMEMS (LAMBDA (L)
  79.   (* return the number of members in the list L *)
  80.   ((NULL L) 0)
  81.   (ADD1 (NMEMS (CDR L))) ))
  82.  
  83. (DEFUN EXTRACT (LAMBDA (K L TMP)
  84.   (* return the Kth member of L *)
  85.   (* TMP is a local variable *)
  86.   (LOOP
  87.     ((ZEROP K) TMP)
  88.     (SETQ K (SUB1 K))
  89.     (SETQ TMP (POP L)) ) ))
  90.  
  91. (DEFUN RESOLVE (LAMBDA (CL1 I CL2 J LLIT RLIT LNDX RNDX BNDEV
  92.     LSIGN RSIGN)
  93.   (* resolve clause CL1 litteral I with clause CL2 litteral J
  94.       returning a new clause record representing the resolvent:
  95.       UNIFY will extend the binding environment: returns NIL if
  96.       impossible *)
  97.   (GETLIT CL1 I)
  98.   (SETQ LLIT LITG)
  99.   (SETQ LNDX INDEXG)
  100.   (SETQ LSIGN SIGNG)
  101.   (GETLIT CL2 J)
  102.   (SETQ RLIT LITG)
  103.   (SETQ RNDX (PLUS INDEXG (MAXNDX CL1)))
  104.   (SETQ RSIGN SIGNG)
  105.   (* create the new clause record *)
  106.   (SETQ BNDEV (LIST CL1 I CL2 J (DIFFERENCE (PLUS (NLITS CL1)
  107.       (NLITS CL2)) 2) (PLUS (MAXNDX CL1) (MAXNDX CL2)) NIL))
  108.   (* test for opposite signs *)
  109.   ((EQ LSIGN RSIGN) NIL)
  110.   (* extend the environment by the unification algorithm *)
  111.   ((UNIFY LLIT LNDX RLIT RNDX) BNDEV)
  112.   NIL ))
  113.  
  114. (DEFUN GETLIT (LAMBDA (CL K)
  115.   (* get the Kth litteral in clause CL: return the litteral
  116.       gotten in LITG: and the associated index in INDEXG *)
  117.   (* if CL is an input clause then extract the Kth litteral and
  118.       set the index to 1 *)
  119.   ((INRECP CL)
  120.     (SETQ LITG (EXTRACT K (LPAR CL)))
  121.     (SETQ SIGNG (EQ K 1))
  122.     (SETQ INDEXG 1) )
  123.   (* if it is in the left parent of the clause then get the
  124.       litteral from the left parent *)
  125.   (* this is true if K is less than the litteral last resolved
  126.       upon in the left parent of the current clause *)
  127.   ((LESSP K (LLIT# CL))
  128.     (GETLIT (LPAR CL) K) )
  129.   (* this is also true if K is less than the number of litterals
  130.       in the left parent but in this case we must adjust K by 1
  131.       *)
  132.   ((LESSP K (NUMLITS (LPAR CL)))
  133.     (GETLIT (LPAR CL) (ADD1 K)) )
  134.   (* if the selected litteral is in the right parent but left of
  135.       the litteral last resolved upon then get the litteral from
  136.       the right parent with the appropriate adjustment to K *)
  137.   ((LESSP K (PLUS (SUB1 (NUMLITS (LPAR CL))) (RLIT# CL)))
  138.     (GETLIT (RPAR CL) (ADD1 (DIFFERENCE K (NUMLITS (LPAR CL)))))
  139.     (* in this case adjust the index got *)
  140.     (SETQ INDEXG (PLUS INDEXG (MAXNDX (LPAR CL)))) )
  141.   (* otherwise the selected litteral is in the right parent to
  142.       the right of last litteral resolved upon so adjust K
  143.       accordingly and get the litteral *)
  144.   (GETLIT (RPAR CL) (PLUS (DIFFERENCE K (NUMLITS (LPAR CL))) 2))
  145.   (* and adjust the index gotten *)
  146.   (SETQ INDEXG (PLUS INDEXG (MAXNDX (LPAR CL)))) ))
  147.  
  148. (DEFUN UNIFY (LAMBDA (TERM1 INDEX1 TERM2 INDEX2)
  149.   (* attempt to unify TERM1 under INDEX1 with TERM2 under INDEX2
  150.       and extend the binding environment represented in the
  151.       global variable BNDEV: return T if successful or NIL if the
  152.       unification is impossible *)
  153.   (* if both terms and indices are equal then return T: no
  154.       extension to BNDEV is needed *)
  155.   ((EQUAL TERM1 TERM2)
  156.     (EQ INDEX1 INDEX2)
  157.     T )
  158.   (* if TERM1 is a variable *)
  159.   ((VARIABLEP TERM1)
  160.     (* then if it is bound in the current environment *)
  161.     ((ISBOUND TERM1 INDEX1 BNDEV)
  162.       (* then substitute that binding and attempt to unify again
  163.           *)
  164.       (UNIFY TERMB INDEXB TERM2 INDEX2) )
  165.     (* else if the variable of TERM1 occurs in TERM2 then we
  166.         have a recursive """black" "hole""" situation so return 
  167.         NIL *)
  168.     ((OCCUR TERM1 INDEX1 TERM2 INDEX2) NIL)
  169.     (* else force a unification by adding the necessary binding
  170.         and return T for success *)
  171.     (BIND TERM1 INDEX1 TERM2 INDEX2 BNDEV)
  172.     T )
  173.   (* if TERM2 is a variable then swap the 2 terms and UNIFY the
  174.       other way *)
  175.   ((VARIABLEP TERM2)
  176.     (UNIFY TERM2 INDEX2 TERM1 INDEX1) )
  177.   (* otherwise if the heads of the terms unify then return the
  178.       result of unifying the tails of the terms: the environment
  179.       is extended as needed *)
  180.   ((UNIFY (CAR TERM1) INDEX1 (CAR TERM2) INDEX2)
  181.     (UNIFY (CDR TERM1) INDEX1 (CDR TERM2) INDEX2) ) ))
  182.  
  183. (DEFUN ISBOUND (LAMBDA (VAR INDEX BNDEV)
  184.   (* determine if the variable VAR under the index INDEX is
  185.       bound in the binding environment BNDEV: if it is then
  186.       return T and set the free variables TERMB and INDEXB to
  187.       the term and index respectively to which it is bound: *)
  188.   (* otherwise return NIL and do not alter the values of TERMB and
  189.       INDEXB *)
  190.   (* if BNDEV is an input record then it cannot be bound so
  191.       return NIL *)
  192.   ((INRECP BNDEV) NIL)
  193.   (* if VAR under INDEX is equal to the head of the binding
  194.       environment at this level then return T and set TERMB and
  195.       INDEXB accordingly *)
  196.   ((EQUAL (CONS VAR INDEX) (CAR (BINDINGS BNDEV)))
  197.     (SETQ TERMB (CADAR (BINDINGS BNDEV)))
  198.     (SETQ INDEXB (CAR (CDDAR (BINDINGS BNDEV))))
  199.     T )
  200.   (* else see if it is bound in the tail of the binding
  201.       environment at this level *)
  202.   ((ISBOUND VAR INDEX (CDR (BINDINGS BNDEV))) T)
  203.   (* if not then check INDEX to see whether to search the left
  204.       or right parent binding environment *)
  205.   ((LEQP INDEX (MAXNDX (LPAR BNDEV)))
  206.     (* search left parent *)
  207.     (ISBOUND VAR INDEX (LPAR BNDEV)) )
  208.   (* search right parent *)
  209.   ((ISBOUND VAR (DIFFERENCE INDEX (MAXNDX (LPAR BNDEV))) (RPAR
  210.       BNDEV))
  211.     (* adjust INDEXB accordingly *)
  212.     (SETQ INDEXB (PLUS INDEXB (MAXNDX (LPAR BNDEV))))
  213.     (* and return success *)
  214.     T )
  215.   (* all possible approaches failed so return NIL for not bound *)
  216.   NIL ))
  217.  
  218. (DEFUN OCCUR (LAMBDA (V I TERM J)
  219.   (* see if the variable V under the index I occurs in the term
  220.       TERM under the index J and return T or NIL *)
  221.   (* if TERM is a variable *)
  222.   ((VARIABLEP TERM)
  223.     (* then if it is bound *)
  224.     ((ISBOUND TERM J BNDEV)
  225.       (* then make the substitution and test for occurance *)
  226.       (OCCUR V I TERMB INDEXB) )
  227.     (* if V equals TERM *)
  228.     ((EQ V TERM)
  229.       (* then return T if I=J else NIL *)
  230.       (EQ I J) ) )
  231.   (* if TERM is atomic and not a variable *)
  232.   (* then it is a constant so return NIL *)
  233.   ((ATOM TERM) NIL)
  234.   (* otherwise if V under I occurs in the head of TERM under J
  235.       then return T *)
  236.   ((OCCUR V I (CAR TERM) J) T)
  237.   (* otherwise return T if V under I occurs in the tail of TERM
  238.       under J and NIL otherwise *)
  239.   (OCCUR V I (CDR TERM) J) ))
  240.  
  241. (DEFUN BIND (LAMBDA (V I TERM J BNDEV)
  242.   (* bind V under I to TERM under J in BNDEV *)
  243.   (SETBINDINGS BNDEV (CONS (CONS (CONS V I) (CONS TERM J))
  244.       (BINDINGS BNDEV))) ))
  245.  
  246. (DEFUN * (LAMBDA COMMENTS
  247.   NIL ))
  248.  
  249. (DEFUN MAKECL (LAMBDA (CL)
  250.   (* make a clause record out of the expression CL *)
  251.   (LIST CL 0 NIL 0 (NMEMS CL) 1 NIL) ))
  252.  
  253.  
  254. (RDS)
  255. AKECL (LAMBDA (CL)
  256.   (* make a clause record out of the expression CL *)
  257.   (LI